<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/6/1
 * Time: 15:49
 */

namespace App\Listener;

use Swoole\Coroutine\Http\Client;
use Swot\Server\Server;
use Swot\Event\Listener;

class StartListener extends Listener
{
    protected $name = 'start';

    public function handler($swotServer = null, $swooleServer = null)
    {
        dd('this is StartListener handler', 'StartListener');
//
//         go(function(){
//             $cli = new \Swoole\Coroutine\Http\Client('10.0.0.200', 9500);
//             $ret = $cli->upgrade("/");
//             $cli->push('1');
//             $cli->close();
//         });

        $config = $this->app->make('config');

        go(function() use ($swotServer, $config){
            $cli = new \Swoole\Coroutine\Http\Client($config->get('server.route.server.host'), $config->get('server.route.server.port'));
            if ($cli->upgrade("/")) {
                // 这是本机信息
                $data = [
                    'ip'         => $swotServer->getHost(),
                    'port'       => $swotServer->getPort(),
                    'serverName' => 'swot_im1',
                    'method'     => 'register',
                ];

                $cli->push(\json_encode($data));
                 \swoole_timer_tick(3000, function() use ($cli){
                     $cli->push('', WEBSOCKET_OPCODE_PING);
//                     $cli->push('1');
                 });
//                // $cli->close();
            }
        });

//        go(function() use ($server) {
//            $cli = new Client('10.0.0.200', 9500);
//            if ($cli->upgrade("/")) {
//                $data = [
//                    'ip'         => $server->getHost(),
//                    'port'       => $server->getPort(),
//                    'serverName' => 'swot_im1',
//                    'method'     => 'register',
//                ];
//                $cli->push(\json_encode($data));
//
//                $cli->close();
//            }
//        });

    }
}